<?
    header('Content-type: text/html; charset=utf-8');
    $error = 0;
    $errormessage = '';
    if (isset($_GET['error'])) {
        $error = $_GET['error'];
    }
    if ($error != '0') {
        $error = 1;
        $errormessage = 'test error message';
    }
    $connection = mysql_connect ('localhost', 'zorik_if039', '9731') or die ('Unable to connect!');
	mysql_select_db('zorik_if039') or die (mysql_error());
	mysql_query("set character_set_client='utf8'"); 
	mysql_query("set character_set_server='utf8'"); 
	mysql_query("set character_set_results='utf8'"); 
	mysql_query("set collation_connection='utf8_unicode_ci'");

    $query = "SELECT * FROM photo";
	$res = mysql_query($query) or die(mysql_error());
	$rows = array();
	while($r = mysql_fetch_assoc($res)) {
        $rows[] = $r;
    }

    $rndNo = rand(0, count($rows) - 1);
    $user_id = $rows[$rndNo]['user_id'];

    $query = "SELECT * FROM user where id='$user_id'";
    $res = mysql_query($query) or die(mysql_error());
    $user = array();
    while($r = mysql_fetch_assoc($res)) {
        $user[] = $r;
    }

    if ($error == 0) {
        $photoList = (object) array('error' => $error, 'errormessage' => $errormessage, 'data' => $rows[$rndNo]);
        $photoList -> data['user'] = (object)($user[0]);
    } else {
        $photoList = (object) array('error' => $error, 'errormessage' => $errormessage, 'data' => '');
    }
    sleep(1);
    echo json_encode($photoList);
    mysql_close($connection);
?>